/**
  *@brief create by vacant
  * use getIpadd and setIpadd to get the current ip address and set the ip address
  */

#include "iplinewidget.h"
#include <QSpinBox>
#include <QHBoxLayout>
#include <QEvent>
#include <QDebug>

IpLineWidget::IpLineWidget(QWidget *parent, QColor color) :
    QWidget(parent),m_bgcolor(color),box1(nullptr),box2(nullptr),box3(nullptr),box4(nullptr)
{
    setObjectName("IpLineWidget");
    QHBoxLayout *hbox = new QHBoxLayout;
    box1 = new QSpinBox(this);
    box2 = new QSpinBox(this);
    box3 = new QSpinBox(this);
    box4 = new QSpinBox(this);
    addBox(box1,hbox,box2);
    addBox(box2,hbox,box3);
    addBox(box3,hbox,box4);
    addBox(box4,hbox,this);
    hbox->setMargin(0);
    hbox->setSpacing(0);
    box4->setSuffix("");

    setLayout(hbox);
    setMinimumWidth(120);

    //inint style
    initStyle();
}

IpLineWidget::~IpLineWidget()
{
    if(box1){
        delete box1;
        box1 = NULL;
    }
    if(box2){
        delete box2;
        box2 = NULL;
    }
    if(box3){
        delete box3;
        box3 = NULL;
    }
    if(box4){
        delete box4;
        box4 = NULL;
    }
}

QString IpLineWidget::getIpadd(int whichbox)
{
    QString str = box1->text()+box2->text()+box3->text()+box4->text();
    switch(whichbox){
    case 1:
        str = QString::number(box1->value());
        break;
    case 2:
        str = QString::number(box1->value());
        break;
    case 3:
        str = QString::number(box1->value());
        break;
    case 4:
        str = QString::number(box1->value());
        break;
    }
    return str;
}

QString IpLineWidget::Text()
{
    if(box1 && box2 && box3 && box4)
        return box1->text()+box2->text()+box3->text()+box4->text();
}

void IpLineWidget::setIpadd(QString str)
{
    QStringList list = str.split(".");
    for(int i = 0;i<list.count();i++){
        switch(i){
        case 0:
            box1->setValue(list.at(i).toInt());
        break;
        case 1:
            box2->setValue(list.at(i).toInt());
            break;
        case 2:
            box3->setValue(list.at(i).toInt());
            break;
        case 3:
            box4->setValue(list.at(i).toInt());
            break;
        }
    }
    setFocus();
}

void IpLineWidget::setBackground(QColor color)
{
    m_bgcolor = color;
    initStyle();
}

void IpLineWidget::setForeground(QColor color)
{
    m_fontcolor = color;
    initStyle();
}

void IpLineWidget::setRange(QSize size,int whichbox)
{
    switch(whichbox){
    case 1:
        box1->setRange(size.width(),size.height());
        break;
    case 2:
        box2->setRange(size.width(),size.height());
        break;
    case 3:
        box3->setRange(size.width(),size.height());
        break;
    case 4:
        box4->setRange(size.width(),size.height());
        break;
    default:
        box1->setRange(size.width(),size.height());
        box2->setRange(size.width(),size.height());
        box3->setRange(size.width(),size.height());
        box4->setRange(size.width(),size.height());
        break;
    }
}

void IpLineWidget::addBox(QSpinBox *box, QLayout *layout, QWidget *nexttab)
{
    static int num = 0;
    box->setObjectName(QString("IpLineWidgetBox_%1").arg(QString::number(num++)));
    layout->addWidget(box);
    box->setButtonSymbols(QAbstractSpinBox::NoButtons);
    box->setSuffix(".");
    box->setAlignment(Qt::AlignCenter);
    box->setRange(0,255);
    box->setMinimumWidth(35);
    box->selectAll();
    connect(box,static_cast<void (QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged),[=]{
        if(box->text().length()>3)
            if(nexttab)
                nexttab->setFocus();
    });
    box->installEventFilter(this);
}

void IpLineWidget::initStyle()
{
    foreach(QSpinBox *box,findChildren<QSpinBox *>()){
        if(box){
            QString boxsty = "QSpinBox{border:1px solid rgb(63,184,250);%1;%2;background:%3;color:%4;}"
                             "QSpinBox:focus,:hover{border-bottom:2px solid rgb(50,149,222);}";
            box->setStyleSheet(boxsty.arg(box->objectName().right(1).toInt()==3?
                                              "border-top-right-radius:2px;border-bottom-right-radius:2px;border-left:0px;":
                                              "border-right:0px",
                                          box->objectName().right(1).toInt()==0?
                                              "border-top-left-radius:2px;border-bottom-left-radius:2px;border-right:0px;":
                                              "border-left:0px;",
                                          m_bgcolor.isValid()?m_bgcolor.name():"white",
                                          m_fontcolor.isValid()?m_fontcolor.name():"black"));
        }
    }
}

bool IpLineWidget::eventFilter(QObject *obj, QEvent *ev)
{
    if(obj == box1){
        if(ev->type() == QEvent::FocusIn){
            box1->selectAll();
            return true;
        }
    }
    else if(obj == box2){
        if(ev->type() == QEvent::FocusIn){
            box2->selectAll();
            return true;
        }
    }
    else if(obj == box3){
        if(ev->type() == QEvent::FocusIn){
            box3->selectAll();
            return true;
        }
    }
    else if(obj == box4){
        if(ev->type() == QEvent::FocusIn){
            box4->selectAll();
            return true;
        }
    }
    return QWidget::eventFilter(obj,ev);
}
